package com.xajh.duoduo.impl.service;

import com.xajh.duoduo.config.switchh.ProcessSwitch;
import com.xajh.duoduo.environment.CurrentRolePosition;
import com.xajh.duoduo.eunm.position.RolePosition;
import com.xajh.duoduo.eunm.process.ProcessEnum;
import com.xajh.duoduo.interfac.Processor;
import com.xajh.duoduo.manager.ProcessorManger;
import com.xajh.duoduo.service.OneProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.atomic.AtomicReference;

@Service
public class OneProcessImpl implements OneProcessService {
    @Autowired
    private ProcessorManger processorManger;

    @Override
    public String oneProcess(String type, String processSwitch, String itemIndex) throws Exception {

        Processor processor = processorManger.initProcessor(type);
        ProcessEnum processEnum = ProcessEnum.getEnumByType(type);
        switch (processEnum){
            //如果是练级，就要判断是停止还是开始
            case UP_LEVEL:
                //如果是开始，检查开关变量是否为false。
                switch (processSwitch){
                    case "true":
                        boolean isProcess = ProcessSwitch.upLevelSwitch.get();
                        // 开关变量如果为true，则将变量设置为false，并等待60s后（为了保证流程停止），
                        // 再将变量设置为true，开始流程
                        if(isProcess){
                            ProcessSwitch.upLevelSwitch.set(false);
                            Thread.sleep(60 * 1000);
                            ProcessSwitch.upLevelSwitch.set(true);
                            processor.process();
                        }else {
                            // 开关变量如果为false，则将变量设置为true，开始流程
                            ProcessSwitch.upLevelSwitch.set(true);
                            processor.process();
                        }
                        break;
                    case "false":
                        //如果是停止，则将开关变量设置为false
                        ProcessSwitch.upLevelSwitch.set(false);
                        break;
                }

                break;
            case TRANS_ROLE:
                //设置当前的role
                CurrentRolePosition.setGroupIndex(0);
                CurrentRolePosition.setItemIndex(Integer.parseInt(itemIndex));
                CurrentRolePosition.setCurrentRolePosition(new AtomicReference<>(CurrentRolePosition.getRolesGroup().get(0).get(Integer.parseInt(itemIndex))));
                processor.process();
                break;
            //如果不是练级，则直接调用
            default:
                processor.process();
                break;
        }


        return null;
    }
}
